package com.easepal802s.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.interfaces.IProMasaChoose;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 工作释压
 * Created by FHB on 2018/3/26.
 */

public class LayoutPMFour extends RelativeLayout implements View.OnClickListener{
    private ImageView mIvQuick;//速按放松
    private ImageView mIvDeep;//深度放松
    private ImageView mIvLong;//久坐释压
    private List<ImageView> mViewList = new ArrayList<>();
    private IProMasaChoose mAction;
    private MyHandler myHandler;
    public LayoutPMFour(Context context) {
        super(context);
    }

    public LayoutPMFour(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setAction(IProMasaChoose action){
        mAction = action;
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        mIvQuick =  findViewById(R.id.zy_masa_iv_quick);
        mIvDeep =  findViewById(R.id.zy_masa_iv_relax);
        mIvLong =  findViewById(R.id.zy_masa_iv_long);
        mViewList.add(mIvQuick);
        mViewList.add(mIvDeep);
        mViewList.add(mIvLong);
        mIvQuick.setOnClickListener(this);
        mIvDeep.setOnClickListener(this);
        mIvLong.setOnClickListener(this);

    }

    public void setSelect(int position) {
        myHandler.obtainMessage(100,position).sendToTarget();
    }
    private void makeChoose(int position){
        if (position == -1) {
            for (int i = 0; i < mViewList.size(); i++) {
                mViewList.get(i).setSelected(false);
            }
        } else {
            for (int i = 0; i < mViewList.size(); i++) {
                if (i == position) {
                    Log.e("getDataPer2","select"+position);
                    mViewList.get(i).setSelected(true);
                } else {

                    mViewList.get(i).setSelected(false);
                }

            }
        }
    }
    private  class MyHandler extends Handler {
        WeakReference<LayoutPMFour> mMainActivityWeakReference;

        MyHandler(LayoutPMFour layoutOne) {
            mMainActivityWeakReference = new WeakReference<>(layoutOne);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutPMFour mLayoutTwo= mMainActivityWeakReference.get();
            int position = (int)msg.obj;
            Log.e("getDataPer","select"+position);
            if (mLayoutTwo != null) {

                makeChoose(position);
            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zy_masa_iv_quick:
                mAction.chooseMassage("速按放松", BleConstant.SUAFS);
                break;
            case R.id.zy_masa_iv_relax:
                mAction.chooseMassage("深度放松",BleConstant.SHENDFS);
                break;
            case R.id.zy_masa_iv_long:
                mAction.chooseMassage("久坐释压",BleConstant.JIUZSY);
                break;
        }

    }
}
